import request from "../utils/request.js";

/**
 * 提交问题
 * @param {Object} questionDTO - 问题信息
 * @returns {Promise<number>} 返回问题ID
 */
export const submitQuestion = (questionDTO) => {
  const userId = uni.getStorageSync("userId");
  return request.post("/api/questions", {
    content: questionDTO.content,
    detail: questionDTO.detail,
    fruitId: questionDTO.fruitId,
  });
};

/**
 * 获取问题列表
 * @param {Object} queryDTO - 查询条件
 * @returns {Promise<Array>} 返回问题列表
 */
export const getQuestionList = (queryDTO) => {
  const userId = uni.getStorageSync("userId");
  return request.get("/api/questions", {
    fruitId: queryDTO.fruitId,
    keyword: queryDTO.keyword,
    type: queryDTO.type,
  });
};

/**
 * 获取问题详情
 * @param {number} id - 问题ID
 * @returns {Promise<Object>} 返回问题详情
 */
export const getQuestionDetail = (id) => {
  const userId = uni.getStorageSync("userId");
  return request.get(`/questions/${id}`, {
    header: {
      userId: userId,
    },
  });
};

/**
 * 点赞/取消点赞问题
 * @param {number} questionId - 问题ID
 * @returns {Promise<boolean>} 返回是否点赞成功
 */
export const likeQuestion = (questionId) => {
  const userId = uni.getStorageSync("userId");
  return request.post(`/questions/${questionId}/like`, null, {
    header: {
      userId: userId,
    },
  });
};

/**
 * 提交回答
 * @param {Object} answerDTO - 回答信息
 * @returns {Promise<number>} 返回回答ID
 */
export const submitAnswer = (answerDTO) => {
  const userId = uni.getStorageSync("userId");
  return request.post("/answers", answerDTO, {
    header: {
      userId: userId,
    },
  });
};

/**
 * 获取问题的回答列表
 * @param {number} questionId - 问题ID
 * @returns {Promise<Array>} 返回回答列表
 */
export const getAnswerList = (questionId) => {
  const userId = uni.getStorageSync("userId");
  return request.get(`/answers/question/${questionId}`, {
    header: {
      userId: userId,
    },
  });
};

/**
 * 点赞/取消点赞回答
 * @param {number} answerId - 回答ID
 * @returns {Promise<boolean>} 返回是否点赞成功
 */
export const likeAnswer = (answerId) => {
  const userId = uni.getStorageSync("userId");
  return request.post(`/answers/${answerId}/like`, null, {
    header: {
      userId: userId,
    },
  });
};
